import { Buffer } from 'buffer'
import fs from 'fs'
import { getFileCode } from './pathManage.js'

function writeImg(imgData, FileFullPath) {
    getFileCode(FileFullPath).then(function () {
        console.log("bu存在")
        //过滤data:URL
        var base64Data = imgData.replace(/^data:image\/\w+;base64,/, "");
        var dataBuffer = Buffer.from(base64Data, 'base64'); // 解码base64
        fs.writeFile(FileFullPath, dataBuffer, function (err) {
            if (err) {
                return console.log("写入失败",err)
            }
            console.log("封面保存成功")
            return "保存成功"
        });

    })

}

function writeAudio(voiceData, FileFullPath) {
    getFileCode(FileFullPath).then(function () {
        //过滤data:URL
        var base64Data = voiceData.replace(/^data:audio\/\w+;base64,/, "");
        var dataBuffer = Buffer.from(base64Data, 'base64'); // 解码base64
        fs.writeFile(FileFullPath, dataBuffer, function (err) {
            if (err) {
                return console.log(err)
            }
            console.log("保存音频成功")
            return "保存成功"
        });
    })
}

export { writeImg, writeAudio }
